package com.example.personalizededucationsystem.service;

import com.example.personalizededucationsystem.utils.VerificationType;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

@Service
public class EmailService {

    private final JavaMailSender mailSender;

    @Autowired
    public EmailService(JavaMailSender mailSender) {
        this.mailSender = mailSender;
    }

    private String generateEmailTemplate(String title, String bodyHtml) {
        return String.format("""
                <html>
                <head>
                  <style>
                    body {
                      background-color: #f2f4f6;
                      font-family: 'Segoe UI', 'Helvetica Neue', sans-serif;
                      padding: 40px;
                      color: #333;
                    }
                    .container {
                      max-width: 600px;
                      margin: auto;
                      background-color: #ffffff;
                      border-radius: 8px;
                      box-shadow: 0 2px 8px rgba(0,0,0,0.05);
                      overflow: hidden;
                    }
                    .header {
                      background-color: #409EFF;
                      padding: 20px;
                      text-align: center;
                      color: #fff;
                      font-size: 24px;
                      font-weight: bold;
                    }
                    .content {
                      padding: 30px;
                    }
                    .code-box {
                      background-color: #f7f8fa;
                      border: 1px dashed #409EFF;
                      padding: 20px;
                      text-align: center;
                      font-size: 28px;
                      font-weight: bold;
                      color: #409EFF;
                      margin: 20px 0;
                      letter-spacing: 4px;
                    }
                    .btn {
                      display: inline-block;
                      padding: 12px 20px;
                      background-color: #409EFF;
                      color: white;
                      text-decoration: none;
                      border-radius: 4px;
                      font-weight: bold;
                      margin: 20px auto;
                      text-align: center;
                    }
                    .footer {
                      font-size: 12px;
                      color: #999;
                      text-align: center;
                      padding: 20px;
                    }
                  </style>
                </head>
                <body>
                  <div class="container">
                    <div class="header">%s</div>
                    <div class="content">
                      %s
                    </div>
                    <div class="footer">
                      &copy; 2025 NoName
                    </div>
                  </div>
                </body>
                </html>
                """, title, bodyHtml);
    }

    public void sendCodeMessage(String to, String usage, String code) {
        String subject = String.format("authNoName: %s 验证码", usage);
        String bodyHtml = String.format("""
                <p>您好，</p>
                <p>您正在进行 <strong>%s</strong> 操作。</p>
                <p>请使用以下验证码完成验证：</p>
                <div class="code-box">%s</div>
                <p>验证码有效期为 <strong>10 分钟</strong>，请勿泄露给他人。</p>
                <p>如非本人操作，请忽略此邮件。</p>
                """, usage, code);

        String htmlContent = generateEmailTemplate("authNoName", bodyHtml);
        sendHtmlMessage(to, subject, htmlContent);
    }

    public void sendFindAccountLink(String to, String resetUrl) {
        String subject = "authNoName: 查找账户";
        String bodyHtml = String.format("""
            <p>您好，</p>
            <p>您正在进行 <strong>忘记用户名或密码</strong> 操作。</p>
            <p>请使用以下链接查找你的账户：</p>
            <p><a href="%s">%s</a></p>
            <p>链接有效期为 <strong>10 分钟</strong>，请勿泄露给他人。</p>
            <p>如非本人操作，请忽略此邮件。</p>
            """, resetUrl, resetUrl);

        String htmlContent = generateEmailTemplate("authNoName", bodyHtml);
        sendHtmlMessage(to, subject, htmlContent);
    }

    public void sendHtmlMessage(String to, String subject, String htmlContent) {
        MimeMessage message = mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
            helper.setFrom("authNoName@163.com");
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(htmlContent, true);
            mailSender.send(message);
        } catch (MessagingException e) {
            throw new RuntimeException("Failed to send HTML email", e);
        }
    }

    public void sendVerificationCode(String to, VerificationType type, String code) {
        String usage = getUsageText(type);
        sendCodeMessage(to, usage, code);
    }

    private String getUsageText(VerificationType type) {
        return switch (type) {
            case REGISTER -> "注册账号";
            case RESET_PASSWORD -> "重置密码";
        };
    }
}
